<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

if(isset($_SESSION['u_nic'])&&isset($_GET['id'])) {
	$tpl->assign(array(
		'iconesleft' => 2,
		'prod' => '0',
		'prod_id' => intval($_GET['id'])
	));
	include('php/lib/panel_inc.php');
	$heberg_current = intval($_GET['id']);
	$infos_heberg = mysql_query("SELECT comptes_heberg.id,
										comptes_heberg.u_nic,
										comptes_heberg.uid_linux,
										comptes_heberg.user_linux,
										comptes_heberg.serveur_mysql,
										comptes_heberg.offre,
										comptes_heberg.etat,
										comptes_heberg.mysql AS c_mysql,
										offre_heberg.bases_mysql AS o_mysql,
										offre_surmesure.bases_mysql as o_mysql_
										FROM comptes_heberg 
										LEFT JOIN offre_heberg
										ON comptes_heberg.offre = offre_heberg.id_offre
										LEFT JOIN offre_surmesure
										ON comptes_heberg.id = offre_surmesure.id_heberg
										WHERE comptes_heberg.id='".$heberg_current."' AND comptes_heberg.u_nic='".$_SESSION['u_nic']."'")or die(mysql_error());
	if(mysql_num_rows($infos_heberg) == 1) {
		$infos_heberg = mysql_fetch_array($infos_heberg);
		if($infos_heberg['o_mysql'] == "") {
			$infos_heberg['o_mysql'] = $infos_heberg['o_mysql_'];
		}
		$tpl->assign(array('etat' => $infos_heberg['etat']));
		if($infos_heberg['etat'] == 1) {
			if($_GET['action'] == 'add') {
				if($infos_heberg['c_mysql'] < $infos_heberg['o_mysql']) {
					if(isset($_POST['base'])) {
							$nom_base = $infos_heberg['user_linux'].'_'.preg_replace('#([^a-z0-9\-\_])+#i', '', $_POST['base']);
							$verif = mysql_fetch_array(query("SELECT COUNT(*) AS verif FROM bases_mysql WHERE nom_base='".$nom_base."'"));
							if($verif['verif'] == 0) {
								query("INSERT INTO bases_mysql VALUES('', '".$infos_heberg['id']."', '".$nom_base."')");
								query("UPDATE comptes_heberg SET mysql=mysql+1 WHERE id='".$infos_heberg['id']."'");
								deconnexionbdd();
								mysql_connect($server[$infos_heberg['serveur_mysql']]['ns'],"root",$server[$infos_heberg['serveur_mysql']]['pass_mysql']);
								query("CREATE DATABASE `".$nom_base."`");
								query("GRANT SELECT , INSERT , UPDATE , DELETE , CREATE , DROP , INDEX , ALTER , CREATE VIEW , SHOW VIEW , CREATE ROUTINE, ALTER ROUTINE, EXECUTE, LOCK TABLES ON `".$nom_base."` . * TO '".$infos_heberg['user_linux']."'@'%'");
								deconnexionbdd();
								connexionbdd();
								header("Location:hebergement-mysql.html?id=".intval($_GET['id']));
							}
							else {
								$title = "Ajout d'une bases MySQL";
								$tplinc = "panel-hebergement-mysql-add";
								$parse = 1;
								$tpl->assign(array(
										'user_linux' => $infos_heberg['user_linux'],
										'erreur' => 1
								));
							}
					}
					else {
						$title = "Ajout d'une bases MySQL";
						$tplinc = "panel-hebergement-mysql-add";
						$parse = 1;
						$tpl->assign(array(
									'user_linux' => $infos_heberg['user_linux']
						));
					}
				}
				else {
					header("Location:hebergement-mysql.html?id=".intval($_GET['id']));
				}
			}
			elseif($_GET['action'] == 'pass') {
				if(isset($_POST['password1'])) {
					if($_POST['password1'] == $_POST['password2']) {
						$pass = mysql_real_escape_string($_POST['password1']);
						deconnexionbdd();
						mysql_connect($server[$infos_heberg['serveur_mysql']]['ns'],"root",$server[$infos_heberg['serveur_mysql']]['pass_mysql']);
						query("UPDATE mysql.user SET password=PASSWORD('".$pass."') WHERE user='". $infos_heberg['user_linux']."'"); 
						query("FLUSH TABLES, PRIVILEGES");	
						deconnexionbdd();
						connexionbdd();
						header("Location:hebergement-mysql.html?id=".intval($_GET['id']));
					}
					else{
						$title = "Modifier le mot de passe MySQL";
						$tplinc = "panel-hebergement-mysql-pass";
						$parse = 1;
						$tpl->assign(array(
							'user_linux' => $infos_heberg['user_linux'],
							'erreur' => 1
						));
					
					}
				}
				else {
					$title = "Modifier le mot de passe MySQL";
					$tplinc = "panel-hebergement-mysql-pass";
					$parse = 1;
					$tpl->assign(array(
							'user_linux' => $infos_heberg['user_linux']
					));
				}
			}
			elseif($_GET['action'] == 'del') {
				$base = intval($_GET['base']);
				$infos_base = query("SELECT * FROM bases_mysql WHERE id='".$base."' AND id_heberg='".$infos_heberg['id']."'");
				if(mysql_num_rows($infos_base) == 1){
					$infos_base = mysql_fetch_array($infos_base);
					query("DELETE FROM bases_mysql WHERE id='".$base."'");
					query("UPDATE comptes_heberg SET mysql = mysql -1 WHERE id='".$infos_heberg['id']."'");
					deconnexionbdd();
					mysql_connect($server[$infos_heberg['serveur_mysql']]['ns'],"root",$server[$infos_heberg['serveur_mysql']]['pass_mysql']);
					query("DROP DATABASE `".$infos_base['nom_base']."`");
					deconnexionbdd();
					connexionbdd();
				}
				header("Location:hebergement-mysql.html?id=".intval($_GET['id']));
			}
			else{ 
				$title = "Gestion des bases MySQL";
				$tplinc = "panel-hebergement-mysql";
				$parse = 1;	
				if($infos_heberg['o_ftp'] == "") {
					$infos_heberg['o_ftp'] = "Illimités";
				}
				$tpl->assign(array(
					"serveur_mysql_i" => $server[$infos_heberg['serveur_mysql']]['ip'],
					"serveur_mysql_n" => $server[$infos_heberg['serveur_mysql']]['ftp'],
					"o_mysql" => $infos_heberg['o_mysql'],
					"c_mysql" => $infos_heberg['c_mysql'],
				));
				$basesmysql = query("SELECT id, nom_base FROM bases_mysql WHERE id_heberg='".$infos_heberg['id']."'");
				While($bmysql = mysql_fetch_array($basesmysql)) {
					$tpl->assign_array('bases_mysql',array(
						'nom_base' => $bmysql['nom_base'],
						'id' => $bmysql['id'],
					));	
				}
			}
		}else { header("Location:/panel/hebergement.html?id=".intval($_GET['id'])); }
	}
	
}
else { header("Location:/site/connexion.html"); }

?>